using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;


namespace NodeCanvas.BehaviourTrees
{
    [Category("Decorators")]
    [Description(
        "Interupts decorated child node and returns Failure if the child node is still Running after the timeout period.")]
    [ParadoxNotion.Design.Icon("Timeout")]
    public class Cooldown : BTDecorator
    {
        [Tooltip("The timeout period in seconds.")]
        public BBParameter<float> timeout = 1;

        protected override Status OnExecute(Component agent, IBlackboard blackboard)
        {
            if (decoratedConnection == null)
            {
                return Status.Optional;
            }

            if (timeout.value - elapsedTime < 0f)
            {
                status = decoratedConnection.Execute(agent, blackboard);
                return status;
            }

            decoratedConnection.Reset();
            return Status.Running;
        }

        ///----------------------------------------------------------------------------------------------
        ///---------------------------------------UNITY EDITOR-------------------------------------------
#if UNITY_EDITOR
        protected override void OnNodeGUI()
        {
            GUILayout.Space(25);
            var pRect = new Rect(5, GUILayoutUtility.GetLastRect().y, rect.width - 10, 20);
            var t = 1 - (elapsedTime / timeout.value);
            UnityEditor.EditorGUI.ProgressBar(pRect, t,
                elapsedTime > 0 ? string.Format("({0})", elapsedTime.ToString("0.0")) : "Ready");
        }

#endif
        ///----------------------------------------------------------------------------------------------
    }
}